1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package com.google.common.collect.testing;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.ListIterator;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  @GwtCompatible
40  public abstract class AbstractMapTester<K, V> extends
41      AbstractContainerTester<Map<K, V>, Map.Entry<K, V>> {
42    protected Map<K, V> getMap() {
43      return container;
44    }
45  
46    @Override public void setUp() throws Exception {
47      super.setUp();
48      samples = this.getSubjectGenerator().samples();
49      resetMap();
50    }
51  
52    @Override protected Collection<Map.Entry<K, V>> actualContents() {
53      return getMap().entrySet();
54    }
55  
56    
57    protected void resetMap() {
58      resetContainer();
59    }
60  
61    protected void expectMissingKeys(K... elements) {
62      for (K element : elements) {
63        assertFalse("Should not contain key " + element,
64            getMap().containsKey(element));
65      }
66    }
67  
68    protected void expectMissingValues(V... elements) {
69      for (V element : elements) {
70        assertFalse("Should not contain value " + element,
71            getMap().containsValue(element));
72      }
73    }
74  
75    
76  
77  
78  
79    protected Map.Entry<K, V>[] createArrayWithNullKey() {
80      Map.Entry<K, V>[] array = createSamplesArray();
81      final int nullKeyLocation = getNullLocation();
82      final Map.Entry<K, V> oldEntry = array[nullKeyLocation];
83      array[nullKeyLocation] = entry(null, oldEntry.getValue());
84      return array;
85    }
86  
87    protected V getValueForNullKey() {
88      return getEntryNullReplaces().getValue();
89    }
90  
91    protected K getKeyForNullValue() {
92      return getEntryNullReplaces().getKey();
93    }
94  
95    private Entry<K, V> getEntryNullReplaces() {
96      Iterator<Entry<K, V>> entries = getSampleElements().iterator();
97      for (int i = 0; i < getNullLocation(); i++) {
98        entries.next();
99      }
100     return entries.next();
101   }
102 
103   
104 
105 
106 
107   protected Map.Entry<K, V>[] createArrayWithNullValue() {
108     Map.Entry<K, V>[] array = createSamplesArray();
109     final int nullValueLocation = getNullLocation();
110     final Map.Entry<K, V> oldEntry = array[nullValueLocation];
111     array[nullValueLocation] = entry(oldEntry.getKey(), null);
112     return array;
113   }
114 
115   protected void initMapWithNullKey() {
116     resetMap(createArrayWithNullKey());
117   }
118 
119   protected void initMapWithNullValue() {
120     resetMap(createArrayWithNullValue());
121   }
122 
123   
124 
125 
126 
127 
128 
129 
130   protected void expectNullKeyMissingWhenNullKeysUnsupported(String message) {
131     try {
132       assertFalse(message, getMap().containsKey(null));
133     } catch (NullPointerException tolerated) {
134       
135     }
136   }
137 
138   
139 
140 
141 
142 
143 
144 
145   protected void expectNullValueMissingWhenNullValuesUnsupported(
146       String message) {
147     try {
148       assertFalse(message, getMap().containsValue(null));
149     } catch (NullPointerException tolerated) {
150       
151     }
152   }
153 
154   @SuppressWarnings("unchecked")
155   @Override protected MinimalCollection<Map.Entry<K, V>>
156       createDisjointCollection() {
157     return MinimalCollection.of(samples.e3, samples.e4);
158   }
159 
160   protected int getNumEntries() {
161     return getNumElements();
162   }
163 
164   protected Collection<Map.Entry<K, V>> getSampleEntries(int howMany) {
165     return getSampleElements(howMany);
166   }
167 
168   protected Collection<Map.Entry<K, V>> getSampleEntries() {
169     return getSampleElements();
170   }
171 
172   @Override protected void expectMissing(Entry<K, V>... entries) {
173     for (Entry<K, V> entry : entries) {
174       assertFalse("Should not contain entry " + entry,
175           actualContents().contains(entry));
176       assertFalse("Should not contain key " + entry.getKey() + " mapped to"
177           + " value " + entry.getValue(),
178           equal(getMap().get(entry.getKey()), entry.getValue()));
179     }
180   }
181 
182   private static boolean equal(Object a, Object b) {
183     return a == b || (a != null && a.equals(b));
184   }
185 
186   
187   protected Entry<K, V> entry(K key, V value) {
188     return Helpers.mapEntry(key, value);
189   }
190 
191   @Override protected void expectContents(Collection<Entry<K, V>> expected) {
192     
193     super.expectContents(expected);
194     for (Entry<K, V> entry : expected) {
195       assertEquals("Wrong value for key " + entry.getKey(),
196           entry.getValue(), getMap().get(entry.getKey()));
197     }
198   }
199 
200   protected final void expectReplacement(Entry<K, V> newEntry) {
201     List<Entry<K, V>> expected = Helpers.copyToList(getSampleElements());
202     replaceValue(expected, newEntry);
203     expectContents(expected);
204   }
205 
206   private void replaceValue(List<Entry<K, V>> expected, Entry<K, V> newEntry) {
207     for (ListIterator<Entry<K, V>> i = expected.listIterator(); i.hasNext();) {
208       if (Helpers.equal(i.next().getKey(), newEntry.getKey())) {
209         i.set(newEntry);
210         return;
211       }
212     }
213 
214     throw new IllegalArgumentException(Platform.format(
215         "key %s not found in entries %s", newEntry.getKey(), expected));
216   }
217 
218   
219 
220 
221 
222 
223 
224   protected V get(K key) {
225     return getMap().get(key);
226   }
227 
228   protected void resetMap(Entry<K, V>[] entries) {
229     resetContainer(getSubjectGenerator().create((Object[]) entries));
230   }
231 }